package functions;
import java.util.ArrayList;

import types.*;

public class CreateFinalOutput {
	// create final set of tests 
	public static FinalTest[] GetRepresentingSet(DataSet ds, ArrayList<SubSet> sets){
		
		int resSize = ds.getSizeRestricted();
		FinalTest[] finalSet = new FinalTest[resSize];
		Boolean noMoreTests = false;
		int numOfTests = 0;
		int addedTests = 0;
		double eps = 0.0;
		double dis = 0.0;
		
		// enter sub-groups medoids as final tests
		for(numOfTests=0; numOfTests<sets.size(); numOfTests++){
			finalSet[numOfTests]= new FinalTest(sets.get(numOfTests).getBestMedoid(), numOfTests);
		}
		numOfTests++;
		
		// if max destination is chosen, enter tests with distance bigger then eps
		// as final tests
		if(ds.isMax())
		{
			eps = ds.getMaxDistance();
			
			if(numOfTests < resSize)
			{
				// Add out-lier tests to final set
				while(numOfTests < resSize && !noMoreTests)
				{
					addedTests = 0;
					
					for(int i=0; (i<sets.size()) && (numOfTests < resSize); i++)
					{
						for(int j=0; j<sets.get(i).getSize(); j++)
						{
							if(!sets.get(i).getElement(j).equals(sets.get(i).getBestMedoid()))
							{
								dis = NCD.getNCD(sets.get(i).getElement(j), sets.get(i).getBestMedoid());
								
								if(dis > eps){
									finalSet[numOfTests] = new FinalTest(sets.get(i).getElement(j), i);
									sets.get(i).deleteElement(j);
									numOfTests++;
									addedTests++;
									j = sets.get(i).getSize();
								}
								else{
									sets.get(i).deleteElement(j);
									j--;
								}
							} //end if
						}// end for
							
					}//end for
					
					if(addedTests==0)	noMoreTests = true;
				}//end while
			
			}//end if
		
		}//end if
		
		
		
		// if standard deviation chosen, choose random tests as final tests
		if(ds.isStd()){
			if(numOfTests < resSize){
				while(numOfTests < resSize){
					for(int i=0; (i<sets.size()) && (numOfTests < resSize); i++)
					{
						for(int j=0; j<sets.get(i).getSize(); j++)
						{
							if(!sets.get(i).getElement(j).equals(sets.get(i).getBestMedoid()))
							{
								finalSet[numOfTests] = new FinalTest(sets.get(i).getElement(j), i);
								sets.get(i).deleteElement(j);
								numOfTests++;
								j = sets.get(i).getSize();
							}
						}
					}
				}
				
			}
		}
	
		return finalSet;
			
	}
	

	
}//end class
